<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 *  用户设置
 */
class UserSetting extends Model
{
    protected $table = 'user_setting';

    protected $fillable = [
        'user_id', 'show_charm', 'show_gift', 'show_location', 'flag'
    ];

    static function permitShow($userId = 0, $key = '')
    {
        $field = "show_{$key}";
        return self::updateOrCreate(['user_id' => $userId], [$field => 1]);
    }

    static function forbidShow($userId = 0, $key = '')
    {
        $field = "show_{$key}";
        return self::updateOrCreate(['user_id' => $userId], [$field => 0]);
    }

    /**
     * 获取权限
     */
    static function getShowPermit($userId = 0, $key = '')
    {
        if (!$data = self::where(['user_id' => $userId])->first()) {
            $ret = [
                'show_charm' => 1,
                'show_gift' => 1,
                'show_location' => 1
            ];
        } else {
            $ret = [
                'show_charm' => $data->show_charm,
                'show_gift' =>  $data->show_gift,
                'show_location' =>  $data->show_location,
            ];
        }
        return !$key ? $ret : $ret["show_{$key}"];
    }
}
